import React, { useState, useEffect } from 'react';
import { Toast } from 'antd-mobile';
import useDictionary from '@/hooks/useDictionary';
import { useRequest } from '@/hooks';
import { sendVerificationCode } from '@/pages/EntryManage/services';

import { Button } from '@/pages/Recruitment/External/components/mobile';
// 邮箱发送结果页
const SendMailResult = ({ email, redirectId }) => {
    const [domain, setDomain] = useState();
    const [disabled, setDisabled] = useState(false);
    const [mailDomainList] = useDictionary('gotoMailbox', {
        useCache: false,
        isAuth: false
    });

    // 发送验证码
    const { run } = useRequest(params => sendVerificationCode(params), {
        manual: true
    });

    useEffect(() => {
        if (email) {
            const arr = email.split('@');
            if (arr.length > 0) {
                const currentArr = mailDomainList?.filter(item => item?.text === `@${arr[1]}`);
                if (currentArr?.length > 0) {
                    setDomain(currentArr[0]?.value);
                }
            }
        }
    }, [email, mailDomainList]);
    const handleUrl = () => {
        window.location.href = domain;
    };
    return (
        <>
            <ul
                style={{
                    fontSize: 14,
                    color: '#858B9B',
                    marginBottom: 45
                }}
            >
                <li>我们已向您的账号邮箱{email}</li>
                <li>发送了一封重置密码的邮件，15分钟有效</li>
            </ul>
            {domain && (
                <div style={{ marginBottom: 16 }}>
                    <Button block size="large" type="primary" target="_blank" onClick={handleUrl}>
                        前往邮箱
                    </Button>
                </div>
            )}

            <div style={{ fontSize: 14, color: '#BCC1CD' }}>
                <span>没有收到？ </span>
                <a
                    disabled={disabled}
                    onClick={async () => {
                        setDisabled(true);
                        const { code, msg } =
                            (await run({
                                loginName: email,
                                organizeType: 1,
                                type: 10,
                                redirectId
                            })) || {};
                        if (code === 10000) {
                            Toast.success('邮件发送成功');
                        } else {
                            Toast.fail(msg);
                        }
                        setDisabled(false);
                    }}
                >
                    重新发送
                </a>
            </div>
        </>
    );
};

export default SendMailResult;
